package com.chenshu.transaction.service;

import com.chenshu.transaction.dao.BankAccountDao;
import com.chenshu.transaction.model.BankAccount;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created with IntelliJ IDEA
 * Description
 * User：chenshu
 * Date：2024-04-10
 * Time：10:21
 */
@Service
@Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, readOnly = true)
public class BankAccountService {
    @Autowired
    private BankAccountDao bankAccountDao;

    /**
     * 转账业务
     * @param fromAccount
     * @param toAccount
     * @param money
     */
    @Transactional(rollbackFor = Exception.class, readOnly = false)
    public void transfer(String fromAccount, String toAccount, int money) {
        //检查转出账户的合法性
        BankAccount fromBankAccount = bankAccountDao.findAccountById(fromAccount);
        if (fromBankAccount == null) {
            System.out.println("转出账户异常，无法进行转账操作");
            return;
        }
        //检查转出账户的余额
        int balance = bankAccountDao.queryBalance(fromAccount);
        if (balance < money) {
            System.out.println("余额不足，无法进行转账操作");
            return;
        }
        //检查转入方账户合法性
        BankAccount toBankAccount = bankAccountDao.findAccountById(toAccount);
        if (toBankAccount == null) {
            System.out.println("转出账户异常，无法进行转账操作");
            return;
        }
        //开始进行转账操作
        try {
            bankAccountDao.updataAccountBalance(fromAccount, -money);
            bankAccountDao.updataAccountBalance(toAccount, money);
            System.out.println("转账成功");
            Integer fromBalance = bankAccountDao.queryBalance(fromAccount);
            Integer toBalance = bankAccountDao.queryBalance(toAccount);
            //检查余额是否合法，如果不合法就抛异常，并回滚
            if (fromBalance < 0 || toBalance < 0) {
                throw new RuntimeException("非法转账操作：需要回滚事务");
            }
            System.out.println(fromAccount+"账户余额为：" + fromBalance);
            System.out.println(toAccount+"账户余额为：" + toBalance);

        } catch (Exception e) {
            System.out.println("转账过程中出现异常：" + e.getMessage() + "; 进行事务回滚！");
            throw new RuntimeException();
        }
    }

    /**
     * 存款业务
     */
    @Transactional(rollbackFor = Exception.class, readOnly = false)
    public void deposit(String depositAccount, int money) {
        //合法性检查
        BankAccount bankAccount = bankAccountDao.findAccountById(depositAccount);
        if (bankAccount == null) {
            System.out.println("存款账户异常");
            return;
        }

        //开始存款操作
        try {
            bankAccountDao.updataAccountBalance(depositAccount, money);
            Integer balance = bankAccountDao.queryBalance(depositAccount);
            //检查余额是否合法
            if (balance < 0) {
                throw new RuntimeException("非法存款操作：需要回滚事务");
            }
            System.out.println("存款成功，" + depositAccount + "的账户余额为：" + balance);
        } catch (Exception e) {
            System.out.println("存款过程出现异常：" + e.getMessage() + "; 需要进行事务回滚！");
        }
    }

    /**
     * 取款业务
     */
    @Transactional(rollbackFor = Exception.class, readOnly = false)
    public void withdraw(String withdrawAccount, int money) {
        //合法性验证
        BankAccount bankAccount = bankAccountDao.findAccountById(withdrawAccount);
        if (bankAccount == null) {
            System.out.println("存款账户异常");
            return;
        }

        //开始取款操作
        try {
            bankAccountDao.updataAccountBalance(withdrawAccount, -money);
            Integer balance = bankAccountDao.queryBalance(withdrawAccount);
            //检查余额是否合法
            if (balance < 0) {
                throw new RuntimeException("非法取款操作：需要回滚事务");
            }
            System.out.println("取款成功，" + withdrawAccount + "的账户余额为：" + balance);
        } catch (Exception e) {
            System.out.println("取款过程出现异常：" + e.getMessage() + "; 需要进行事务回滚！");
        }

    }

    //查询余额
    @Transactional(rollbackFor = Exception.class, readOnly = true)
    public void selectBalance(String account) {
        //合法性验证
        BankAccount bankAccount = bankAccountDao.findAccountById(account);
        if (bankAccount == null) {
            System.out.println("查询账户异常");
            return;
        }

        //开始查询操作
        try {
            Integer balance = bankAccountDao.queryBalance(account);
            System.out.println("当前账户" + account + "的余额为：" + balance);
        } catch (Exception e) {
            System.out.println("查询过程出现异常：" + e.getMessage() + "; 需要进行事务回滚！");
        }
    }

    @Transactional(rollbackFor = Exception.class, readOnly = false)
    public void updatePassword(String account, String newPassword, String rePassword) {
        //判读两次密码输入是否相同
        if (!newPassword.equals(rePassword)) {
            System.out.println("两次输入密码不一致");
            return;
        }
        //合法性验证
        BankAccount bankAccount = bankAccountDao.findAccountById(account);
        if (bankAccount == null) {
            System.out.println("查询账户异常");
            return;
        }

        //开始修改密码操作
        try {
            bankAccountDao.updateAccountPassword(account, newPassword);
            System.out.println("修改密码成功");
        } catch (Exception e) {
            System.out.println("修改密码过程出现异常：" + e.getMessage() + "; 需要进行事务回滚！");
        }

    }
}
